<html>
<head>
  <title>
    Emscripten: Lua
  </title>
  <link id="bespin_base" href="skywriter/"/>
  <script src="skywriter/BespinEmbedded.js"></script>
  <style type="text/css">
      .bespin {
          width: 80%;
          height: 30%;
      }
  </style>
  <script src="lua.js"></script>
  <script>
    // print function which the Lua engine will call
    var lines = [], printed = false;

    function print(text) {
      lines.push(text);
      printed = true;
    }

    function execute(text) {
      lines = [];
      printed = false;

      raw_argv[8] = Pointer_make(intArrayFromString(text), 0, ALLOC_STATIC); // leak!
      argv = Pointer_make(raw_argv, null);
      __Z7runargsP9lua_StatePPci(GLOBAL_L, argv, argc)

      if (!printed) {
        print('<small><i>(no output)</i></small>');
      }

      var element = document.getElementById('output');
      if (!element) return; // perhaps during startup
      element.innerHTML = lines.join('<br>') + '<hr>' + element.innerHTML;
    }

    var editor;

    function doRun() {
      args = ['-e', ''];
      run(args);

      setTimeout(function() { 
        if (!bespin.useBespin) setTimeout(arguments.callee, 10);
        bespin.useBespin(document.getElementById('the_input'), { "stealFocus":true, "syntax": "lua" }).then(function(env) {
          editor = env.editor;
        });
      }, 10);
    }

  </script>
</head>
<body onload="doRun(); document.getElementById('the_input').focus()">
  <p>
    This is the <a href="http://www.lua.org/">Lua</a> interpreter, compiled from C to JavaScript using <a href="http://emscripten.org">Emscripten</a>,
    running in your browser (without any plugins).
  </p>
  <p>
    <ul>
      <li>Most stuff should work, please report bugs if you find them!</li>
      <li>Note that this is an unoptimized build (see <a href="http://code.google.com/p/emscripten/issues/detail?id=8">issue 8</a>)</li>
      <li>The editor is <a href="https://mozillalabs.com/skywriter/">Skywriter</a>. Would be cool if someone made a syntax highlighting plugin for Lua...
    </ul>
  </p>
  <hr>
  <!-- Call Lua's execution function -->
  <form onsubmit="execute(editor.value); return false">
    <b>Enter some Lua</b>:
    <input type="submit" value="execute">
    <div id="the_input">
print("Hello world! This is: " .. _VERSION);

for i=1,5 do
  print("A number: " .. i)
end
    </div>
  </form>
  <hr>
  <div id="output" style="font-family: Courier New,Courier,monospace;"></div>
</body>
</html>

